/**
 * JAVACC DEMO 1.0
 */
package com.apache.client.servlet;

import com.apache.api.vo.ResultMsg;
import com.apache.client.InfoFavoriteCoreClient;
import com.apache.client.InfoMQCoreClient;
import com.apache.client.InfoReleaseCoreCleint;
import com.apache.database.model.Page;
import com.apache.info.util.InfoMap;
import com.apache.tools.DateUtils;
import com.apache.tools.StrUtil;
import com.apache.uct.common.ToolsUtil;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * description:  收藏/消息Action
 * doCode=Fsave:添加收藏信息; doCode=Fedit:修改收藏信息;	doCode=Fdel:删除收藏信息;
 * doCode=Fpages:查收藏列表(pageSize,pageIndex赋值则分页); doCode=Fcount:查收藏数量;
 * doCode=:查信息详细(需要传sortCode及信息ID); doCode=Msave:添加消息; doCode=Medit:修改消息;
 * doCode=Mdel:删除消息; doCode=Mpages:查消息列表(pageSize,pageIndex赋值则分页);
 * doCode=Mcount:查消息数量;	doCode=Mview:查消息详细;
 *
 * @author HouDayu 创建时间：2016-11-18
 */
@SuppressWarnings("serial")
public class FavoriteClientAction extends InfoReleaseActionSupport {

    private Logger logger = LoggerFactory.getLogger(getClass().getName());

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String doCode = request.getParameter("doCode");
        if (StrUtil.isNull(doCode)) {
            defaultMethod(request, response);
            return;
        }
        try {
            Method method = FavoriteClientAction.class.getDeclaredMethod(doCode,
                    new Class[] { HttpServletRequest.class, HttpServletResponse.class });
            method.invoke(this, request, response);
        } catch (Exception e) {
            logger.error("异常",e);
            JSONObject json = new JSONObject();
            json.put("flag", "F");
            json.put("msg", "系统执行异常,请稍后重试");
            outputJson(json.toString(), response);
        }
    }

    //默认方法(按信息ID获取详细信息)
    public void defaultMethod(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Map<String, String> params = this.getRequestMap(request);
        if (ToolsUtil.isNotNull(params.get("sortCode"))) {
            Object favorite = InfoFavoriteCoreClient.getInstance().selectSingle(params);
            JSONObject array = JSONObject.fromObject(favorite);
            outputJson(array.toString(), response);
        }
    }

    //查消息详细
    public void Mview(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Map<String, String> params = this.getRequestMap(request);
        params.put("sortCode", "notice");
        Object notice = InfoMQCoreClient.getInstance().selectSingle(params);
        if (null != notice) {
            InfoMap<Object> info = (InfoMap<Object>) notice;
            if ("0".equals(String.valueOf(info.get("noticeStatus")))) {//修改消息为已读
                String sysPass = params.get("sysPass");
                params.clear();
                params.put("sysPass", sysPass);
                params.put("sortCode", "notice");
                params.put("noticeId", String.valueOf(info.get("noticeId")));
                params.put("noticeStatus", "1");
                InfoReleaseCoreCleint.getInstance().updateInfo(params);
            }
            JSONObject array = JSONObject.fromObject(notice);
            outputJson(array.toString(), response);
        }
    }

    //添加收藏信息
    public void Fsave(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Map<String, String> params = this.getRequestMap(request);
        params.put("createTime", DateUtils.getNow());
        ResultMsg result = saveInfo(request, params, "keep_info");
        JSONObject array = JSONObject.fromObject(result);
        outputJson(array.toString(), response);
    }

    //修改收藏信息
    public void Fedit(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Map<String, String> params = this.getRequestMap(request);
        ResultMsg result = editInfo(request, params, "keep_info");
        JSONObject array = JSONObject.fromObject(result);
        outputJson(array.toString(), response);
    }

    //删除收藏信息
    public void Fdel(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Map<String, String> params = this.getRequestMap(request);
        ResultMsg result = delInfo(request, params, "keep_info");
        JSONObject array = JSONObject.fromObject(result);
        outputJson(array.toString(), response);
    }

    //获取收藏信息数量
    public void Fcount(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Map<String, String> params = this.getRequestMap(request);
        params.put("userEname", this.getSessionUser(request).getUserEname());
        params.put("sortCode", "keep_info");
        long fcount = InfoFavoriteCoreClient.getInstance().countInfo(params);
        outputJson(String.valueOf(fcount), response);
    }

    //添加消息信息
    public void Msave(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Map<String, String> params = this.getRequestMap(request);
        ResultMsg result = saveInfo(request, params, "notice");
        JSONObject array = JSONObject.fromObject(result);
        outputJson(array.toString(), response);
    }

    //修改消息信息
    public void Medit(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Map<String, String> params = this.getRequestMap(request);
        ResultMsg result = editInfo(request, params, "notice");
        JSONObject array = JSONObject.fromObject(result);
        outputJson(array.toString(), response);
    }

    //删除消息信息
    public void Mdel(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Map<String, String> params = this.getRequestMap(request);
        ResultMsg result = delInfo(request, params, "notice");
        JSONObject array = JSONObject.fromObject(result);
        outputJson(array.toString(), response);
    }

    //获取消息信息数量
    public void Mcount(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Map<String, String> params = this.getRequestMap(request);
        params.put("userEname", this.getSessionUser(request).getUserEname());
        params.put("sortCode", "notice");
        long fcount = InfoFavoriteCoreClient.getInstance().countInfo(params);
        outputJson(String.valueOf(fcount), response);
    }

    //获取收藏信息列表
    public void Fpages(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Map<String, String> params = this.getRequestMap(request);
        params.put("sortCode", "keep_info");
        Object favoritePage = null;
        Map<String, Object> map = new HashMap<String, Object>();
        String pageSize = StrUtil.doNull(params.get("pageSize"), params.get("rows"));
        if (ToolsUtil.isNotNull(pageSize)) {
            String pageIndex = StrUtil.doNull(params.get("pageIndex"), params.get("page"));
            params.put("pageIndex", StrUtil.doNull(pageIndex, "1"));
            params.put("pageSize", pageSize);
            Page page = (Page) InfoFavoriteCoreClient.getInstance().selectInfo(params);
            map.put("pageSize", page.getPageSize());
            map.put("pageIndex", page.getPageNeeded());
            map.put("total", page.getCount());
            map.put("rows", page.getPageObjects());
        } else {
            favoritePage = InfoFavoriteCoreClient.getInstance().selectInfo(params);
            if (null == favoritePage) {
                map.put("total", "0");
            } else {
                map.put("total", ((List) favoritePage).size());
            }
            map.put("rows", favoritePage);
        }
        JSONObject array = JSONObject.fromObject(map);
        outputJson(array.toString(), response);
    }

    //获取消息信息列表
    public void Mpages(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Map<String, String> params = this.getRequestMap(request);
        params.put("sortCode", "notice");
        Object favoritePage = null;
        Map<String, Object> map = new HashMap<String, Object>();
        String pageSize = StrUtil.doNull(params.get("pageSize"), params.get("rows"));
        if (ToolsUtil.isNotNull(pageSize)) {
            String pageIndex = StrUtil.doNull(params.get("pageIndex"), params.get("page"));
            params.put("pageIndex", StrUtil.doNull(pageIndex, "1"));
            params.put("pageSize", pageSize);
            Page page = (Page) InfoFavoriteCoreClient.getInstance().selectInfo(params);
            map.put("pageSize", page.getPageSize());
            map.put("pageIndex", page.getPageNeeded());
            map.put("total", page.getCount());
            map.put("rows", page.getPageObjects());
        } else {
            favoritePage = InfoFavoriteCoreClient.getInstance().selectInfo(params);
            if (null == favoritePage) {
                map.put("total", "0");
            } else {
                map.put("total", ((List) favoritePage).size());
            }
            map.put("rows", favoritePage);
        }
        JSONObject array = JSONObject.fromObject(map);
        outputJson(array.toString(), response);
    }

}
